/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	ViewOrganization.java
 * 模块说明：	
 * 修改历史：
 * 2006-6-27 - lxm - 创建。
 */
package com.hd123.rumba.action.organization;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.action.domain.ViewDomain;
import com.hd123.rumba.action.log.ViewEntityLogs;
import com.hd123.rumba.action.user.ViewRole;
import com.hd123.rumba.action.user.ViewUser;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.log.ActionLogger;
import com.hd123.rumba.organization.COrganization;
import com.hd123.rumba.organization.IOrganization;
import com.hd123.rumba.organization.biz.BOrganization;
import com.hd123.rumba.organization.biz.IBOrganizationMgr;
import com.hd123.rumba.organization.biz.QOrganization;
import com.hd123.rumba.organization.biz.SOrganization;
import com.hd123.rumba.request.Action;
import com.hd123.rumba.request.ActionAbstractImpl;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.request.JspLib;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.OperateContext;
import com.hd123.rumba.sql.biz.BResultPage;
import com.hd123.rumba.sql.biz.BSQLSubquery;
import com.hd123.rumba.user.IPermissionMgr;
import com.hd123.rumba.user.IUser;
import com.hd123.rumba.user.PE;
import com.hd123.rumba.util.ClassifiedHashtable;

/**
 * 组织 | Action | 基类
 * 
 * @author lxm
 * 
 */
public abstract class BaseViewOrganization extends ActionAbstractImpl {

  /* Templates */
  protected static final String TEMPLATE_LIST = "organization/OrganizationsList.jsp";
  protected static final String TEMPLATE_FILTER = "organization/OrganizationsFilter.jsp";
  protected static final String TEMPLATE_CREATE = "organization/OrganizationCreate.jsp";
  protected static final String TEMPLATE_EDIT = "organization/OrganizationEdit.jsp";
  protected static final String TEMPLATE_VIEW = "organization/OrganizationView.jsp";
  protected static final String TEMPLATE_BROWSERDEF = "organization/OrganizationBrowserDef.jsp";

  public String list() throws Exception {
    logger.info(ActionLogger.EVENT_LIST, null);

    OrganizationFilter filter = getFormFilter();
    Boolean isRecycle = Boolean.FALSE;
    return this.showListSafely(filter, isRecycle);
  }

  public String listRecycle() throws Exception {
    logger.info(ActionLogger.EVENT_RECYCLE_LIST, null);

    OrganizationFilter filter = getFormFilter();
    Boolean isRecycle = Boolean.TRUE;
    return this.showListSafely(filter, isRecycle);
  }

  public String filter() throws Exception {
    OrganizationFilter filter = getFormFilter();
    String isRecycleStr = (String) dd.getFormValue("isRecycle");
    if (isRecycleStr == null)
      isRecycleStr = "FALSE";
    Boolean isRecycle = "TRUE".equalsIgnoreCase(isRecycleStr) ? Boolean.TRUE : Boolean.FALSE;

    dd.put("filter", filter);
    dd.put("isRecycle", isRecycle);
    dd.setWorkingTemplate(TEMPLATE_FILTER);
    return ActionConstants.APPLICATIONFRAME;
  }

  public String create() throws Exception {
    logger.info(ActionLogger.EVENT_CREATE, null);
    String upperOrgUuid = (String) dd.getFormValue("upperOrg_uuid");
    if ("".equals(upperOrgUuid))
      upperOrgUuid = null;

    BOrganization org = getBOrgMgr().createOrganization(upperOrgUuid);

    return showDetailSafely(org, JspLib.VALUE_FORMSTATE_CREATE, true);
  }

  public String view() throws Exception {
    logger.info(ActionLogger.EVENT_VIEW, null);

    String uuid = (String) dd.getFormValue("org_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数org_uuid。");
    String tabView = (String) dd.getFormValue("tabView");
    if (tabView == null)
      tabView = "tvLogs";

    BOrganization org = getBOrgMgr().loadOrganization(uuid);
    if (!checkToViewOrganization(org)) {
      dd.setWorkingTemplate(ActionConstants.NOACCESS);
      return ActionConstants.APPLICATIONFRAME;
    }

    dd.put("tabView", tabView);
    dd.put("entriesValidation", this.getEntriesValidation(uuid));
    return showDetailSafely(org, JspLib.VALUE_FORMSTATE_VIEW, true);
  }

  public String edit() throws Exception {
    logger.info(ActionLogger.EVENT_EDIT, null);

    String uuid = (String) dd.getFormValue("org_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数org_uuid。");
    String ocaStr = (String) dd.getFormValue("org_oca");
    if (ocaStr == null)
      throw new Exception("缺少必须的参数org_oca。");
    long oca = Long.valueOf(ocaStr).longValue();

    BOrganization org = getBOrgMgr().loadOrganization(uuid);
    
    this.checkOca(org, oca, COrganization.CAPTION_CLASS);

    return showDetailSafely(org, JspLib.VALUE_FORMSTATE_EDIT, true);
  }

  public String save() throws Exception {
    String formState = (String) dd.getFormValue("formState");
    String uuid = (String) dd.getFormValue("org_uuid");
    if ("".equals(uuid))
      uuid = null;
    BOrganization org = null;
    boolean isNew;

    try {
      if (uuid == null) {
        String upperOrgUuid = (String) dd.getFormValue("org_upperOrg_uuid");
        if ("".equals(upperOrgUuid))
          upperOrgUuid = null;
        org = getBOrgMgr().createOrganization(upperOrgUuid);
        isNew = true;
      } else {
        org = getBOrgMgr().loadOrganization(uuid);
        isNew = false;
      }
      readFormOrganization(org, formState);
      if (isNew)
        logger.info(ActionLogger.EVENT_SAVENEW, org.getNameCode());
      else
        logger.info(ActionLogger.EVENT_SAVE, "uuid=\"" + org.getUuid() + "\"。");
      if (!dd.isFormValid())
        return showDetailSafely(org, formState, false);
      ClassifiedHashtable h = getBOrgMgr().saveOrganizationT(org,
          new OperateContext(getCurrentUser()));
      if (h != null && h.size() > 0) {
        dd.addInvalidFormValues("org_", h);
        // oca错误需要特殊处理：取消所做的修改。
        if (h.getMessage("oca") != null && uuid != null) {
          // 断言条件uuid != null，即非新增状态。
          dd.putFormValue("org_uuid", org.getUuid());
          return view();
        } else
          return showDetailSafely(org, formState, false);
      }
    } catch (Exception e) {
      dd.addErrorText(e.getMessage());
      return showDetailSafely(org, formState, false);
    }

    dd.putFormValue("org_uuid", org.getUuid());
    dd.addHint(COrganization.CAPTION_CLASS + "已保存。");
    return view();
  }

  public String delete() throws Exception {
    String uuid = (String) dd.getFormValue("org_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数org_uuid。");
    String ocaStr = (String) dd.getFormValue("org_oca");
    if (ocaStr == null)
      throw new Exception("缺少必须的参数org_oca。");
    long oca = Long.valueOf(ocaStr).longValue();

    try {
      delete2(uuid, oca);
    } catch (Exception e) {
      dd.addErrorText(e.getMessage());
    }
    BOrganization org = getBOrgMgr().loadOrganization(uuid);
    dd.addHint(COrganization.CAPTION_CLASS + "已被删除。");
    dd.put("entriesValidation", this.getEntriesValidation(uuid));
    return showDetailSafely(org, JspLib.VALUE_FORMSTATE_VIEW, false);
  }

  public String undelete() throws Exception {
    String uuid = (String) dd.getFormValue("org_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数org_uuid。");
    String ocaStr = (String) dd.getFormValue("org_oca");
    if (ocaStr == null)
      throw new Exception("缺少必须的参数org_oca。");
    long oca = Long.valueOf(ocaStr).longValue();

    try {
      undelete2(uuid, oca);
    } catch (Exception e) {
      dd.addErrorText(e.getMessage());
    }
    BOrganization org = getBOrgMgr().loadOrganization(uuid);
    dd.addHint(COrganization.CAPTION_CLASS + "已被恢复删除。");
    dd.put("entriesValidation", this.getEntriesValidation(uuid));
    return showDetailSafely(org, JspLib.VALUE_FORMSTATE_VIEW, false);
  }

  public String browse() throws Exception {
    dd.put("entityCaption", COrganization.CAPTION_CLASS);
    dd.put("entityIcon", "rumba/images/organization/org_small.gif");
    dd.setWorkingTemplate(TEMPLATE_BROWSERDEF);
    return ActionConstants.BROWSERFRAME;
  }

  public String viewOrgDomain() throws Exception {
    String uuid = (String) dd.getFormValue("org_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数org_uuid。");

    BOrganization org = getBOrgMgr().loadOrganization(uuid);
    if (org == null)
      throw new Exception("找不到指定的" + COrganization.CAPTION_CLASS + "(" + COrganization.CAPTION_UUID
          + "=" + uuid + ")。");

    dd.putFormValue("start", "view");
    dd.putFormValue("domain_uuid", org.getOrganizationDomain().getUuid());

    Action viewDomain = new ViewDomain();
    viewDomain.init(dd);
    return viewDomain.execute();
  }

  public String viewUsers() throws Exception {
    String uuid = (String) dd.getFormValue("org_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数org_uuid。");

    BOrganization org = getBOrgMgr().loadOrganization(uuid);
    if (org == null)
      throw new Exception("找不到指定的" + COrganization.CAPTION_CLASS + "(" + COrganization.CAPTION_UUID
          + "=" + uuid + ")。");

    dd.putFormValue("start", "list");
    dd.putFormValue("domain_uuid", org.getOrganizationDomain().getUuid());

    Action viewUser = new ViewUser();
    viewUser.init(dd);
    return viewUser.execute();
  }

  public String viewRoles() throws Exception {
    String uuid = (String) dd.getFormValue("org_uuid");
    if (uuid == null)
      throw new Exception("缺少必须的参数org_uuid。");

    BOrganization org = getBOrgMgr().loadOrganization(uuid);
    if (org == null)
      throw new Exception("找不到指定的" + COrganization.CAPTION_CLASS + "(" + COrganization.CAPTION_UUID
          + "=" + uuid + ")。");

    dd.putFormValue("start", "list");
    dd.putFormValue("domain_uuid", org.getOrganizationDomain().getUuid());

    Action viewRole = new ViewRole();
    viewRole.init(dd);
    return viewRole.execute();
  }

  /**
   * 用于汇总页面用户列表的查询。
   * 
   * @param bsq
   * @param pageSize
   * @param page
   * @return
   * @throws Exception
   */
  public BResultPage queryList(BSQLSubquery bsq, int pageSize, int page) throws Exception {
    try {
      addForceFilter(bsq);
      return this.query(bsq, pageSize, page);
    } catch (Exception e) {
      this.reportError("", e);
      throw e;
    }
  }

  /**
   * 根据代码取得组织。
   * 
   * @param code
   * @return
   * @throws Exception
   */
  public SOrganization getOrganizationByCode(String code) throws Exception {
    try {
      IUser currentUser = this.getCurrentUser();
      IOrganization currentOrg = this.getCurrentOrganization();
      BOrganization borg = (BOrganization) getBOrgMgr().getOrganizationByCode(code,
          BOrganization.class);
      if (borg == null || !checkForceFilter(borg, currentUser, currentOrg))
        return null;
      else {
        return borg.cloneSimple();
      }
    } catch (Exception e) {
      this.reportError("", e);
      throw e;
    }
  }

  /**
   * 删除指定的组织。
   * 
   * @param orgUuid
   *          指定被删除组织的uuid。
   * @param oca
   * @throws Exception
   */
  public void delete2(String orgUuid, long oca) throws Exception {
    try {
      logger.info(ActionLogger.EVENT_DELETE, "uuid=\"" + orgUuid + "\"。");
      getBOrgMgr().deleteOrganizationT(orgUuid, new OperateContext(this.getCurrentUser()), oca);
    } catch (Exception e) {
      this.reportError("", e);
      throw e;
    }
  }

  /**
   * 恢复删除指定的组织。
   * 
   * @param orgUuid
   *          指定被恢复删除的组织的uuid。
   * @param oca
   * @throws Exception
   */
  public void undelete2(String orgUuid, long oca) throws Exception {
    try {
      logger.info(ActionLogger.EVENT_UNDELETE, "uuid=\"" + orgUuid + "\"。");
      getBOrgMgr().undeleteOrganizationT(orgUuid, new OperateContext(this.getCurrentUser()), oca);
    } catch (Exception e) {
      this.reportError("", e);
      throw e;
    }
  }

  public OrgEntriesValidation getEntriesValidation(String orgUuid) throws Exception {
    try {
      OrgEntriesValidation oev = new OrgEntriesValidation();
      BOrganization borg = getBOrgMgr().loadOrganization(orgUuid);

      ViewUser viewUser = new ViewUser();
      viewUser.init(dd);
      String permUsers = viewUser.getPermission(ViewUser.FUNCTION_VIEW, borg
          .getOrganizationDomain().getUuid(), Boolean.FALSE);
      if (permUsers != null && !this.getCurrentUser().hasPermission(permUsers))
        oev.setUsers(Boolean.FALSE);

      ViewRole viewRole = new ViewRole();
      viewRole.init(dd);
      String permRoles = viewRole.getPermission(ViewRole.FUNCTION_VIEW, borg
          .getOrganizationDomain().getUuid(), Boolean.FALSE);
      if (!this.getCurrentUser().hasPermission(permRoles))
        oev.setRoles(Boolean.FALSE);

      return oev;
    } catch (Exception e) {
      this.reportError("", e);
      throw e;
    }
  }

  private IBOrganizationMgr borgMgr = null;

  protected IBOrganizationMgr getBOrgMgr() throws BusinessException {
    if (borgMgr == null)
      borgMgr = (IBOrganizationMgr) NamingMgr.getInstance().lookupMgr(IBOrganizationMgr.class);
    return borgMgr;
  }

  /**
   * 根据功能等信息取得权限ID。
   * 
   * @param functionName
   *          功能名，取值为类似“FUNCTION_*”的常量。
   * @param orgUuid
   *          指定访问组织的uuid。
   * @param isRecycle
   *          是否是回收站
   * @return 返回权限ID，若没有权限对应则返回null。
   * @throws BusinessException
   * @throws HibernateException
   */
  public abstract String getPermission(String functionName, String orgUuid, Boolean isRecycle)
      throws BusinessException, HibernateException;

  /**
   * 根据功能等信息取得权限ID。
   * 
   * @param functionName
   *          功能名，取值为类似“FUNCTION_*”的常量。
   * @param relationship
   *          指定与当前登录组织的关系。
   * @param isRecycle
   *          是否是回收站
   * @return 返回权限ID，若没有权限对应则返回null。
   * @throws BusinessException
   * @throws HibernateException
   */
  public abstract String getPermission(String functionName, int relationship, Boolean isRecycle)
      throws BusinessException, HibernateException;

  /**
   * 添加对用户的强制筛选条件。
   * 
   * @param bsq
   */
  protected abstract void addForceFilter(BSQLSubquery bsq) throws BusinessException,
      HibernateException;

  /**
   * 检查强制筛选条件。
   * 
   * @param borg
   *          被检查的组织。
   * @param currentUser
   *          当前登录用户。
   * @param currentOrg
   *          当前登录组织。
   * @return
   * @throws Exception
   */
  protected abstract boolean checkForceFilter(BOrganization borg, IUser currentUser,
      IOrganization currentOrg) throws Exception;

  /**
   * 取得当前列表页面显示组织与当前登录组织的关系。
   * 
   * @param filter
   *          列表页面对应的筛选对象。
   * @return
   * @throws BusinessException
   * @throws HibernateException
   */
  protected abstract int getListRelationship(OrganizationFilter filter) throws BusinessException,
      HibernateException;

  private OrganizationFilter getFormFilter() throws Exception {
    OrganizationFilter filter = new OrganizationFilter();
    filter.setCodeLike((String) dd.getFormValue("filter_codeLike"));
    filter.setNameLike((String) dd.getFormValue("filter_nameLike"));
    String upperOrgUuid = (String) dd.getFormValue("filter_upperOrg");
    if (upperOrgUuid != null) {
      QOrganization qorg = (QOrganization) getBOrgMgr().getOrganization(upperOrgUuid,
          QOrganization.class);
      filter.setUpperOrg(qorg);
    }
    String exceptOrgUuid = (String) dd.getFormValue("filter_exceptOrg");
    if (exceptOrgUuid != null) {
      QOrganization qorg = (QOrganization) getBOrgMgr().getOrganization(exceptOrgUuid,
          QOrganization.class);
      filter.setExceptOrg(qorg);
    }
    String stateStr = (String) dd.getFormValue("filter_state");
    if (stateStr != null)
      filter.setState(new Integer(stateStr));
    return filter;
  }

  private String showListSafely(OrganizationFilter filter, Boolean isRecycle) throws Exception {
    PE peSelf = PE.P(this.getPermission(ViewOrganization.FUNCTION_VIEW,
        IBOrganizationMgr.VALUE_RELATIONSHIP_SELF, isRecycle));
    PE peUpper = PE.P(this.getPermission(ViewOrganization.FUNCTION_VIEW,
        IBOrganizationMgr.VALUE_RELATIONSHIP_UPPER, isRecycle));
    PE peBrother = PE.P(this.getPermission(ViewOrganization.FUNCTION_VIEW,
        IBOrganizationMgr.VALUE_RELATIONSHIP_BROTHER, isRecycle));
    PE peLower = PE.P(this.getPermission(ViewOrganization.FUNCTION_VIEW,
        IBOrganizationMgr.VALUE_RELATIONSHIP_LOWER, isRecycle));
    PE peOther = PE.P(this.getPermission(ViewOrganization.FUNCTION_VIEW,
        IBOrganizationMgr.VALUE_RELATIONSHIP_OTHER, isRecycle));
    PE pe = PE.OR(new PE[] {peSelf, peUpper, peBrother, peLower, peOther});
    if (!this.getCurrentUser().hasPerm(pe)) {
      if (isRecycle.booleanValue()) 
        return showListSafely(filter, Boolean.valueOf(!isRecycle.booleanValue()));
      else
        return showNoAccess();
    }
    return showList(filter, isRecycle);
  }

  private String showList(OrganizationFilter filter, Boolean isRecycle) throws Exception {
    if (!isRecycle.booleanValue())
      logger.info(ActionLogger.EVENT_LIST, null);
    else
      logger.info(ActionLogger.EVENT_RECYCLE_LIST, null);

    dd.put("filter", filter);
    dd.put("isRecycle", isRecycle);
    int relationship = this.getListRelationship(filter);
    this.putFormPermissions(relationship, isRecycle);
    dd.setWorkingTemplate(TEMPLATE_LIST);
    return ActionConstants.APPLICATIONFRAME;
  }
  
  private String showDetailSafely(BOrganization org, String formState, boolean directly) throws Exception {
    if (org == null)
      org = getBOrgMgr().createOrganization((String) dd.getFormValue("upperOrg_uuid"));
    Boolean isRecycle = COrganization.VALUE_STATE_DELETED.equals(org.getState()) ? Boolean.TRUE
        : Boolean.FALSE;
    String funcName = null;
    if (JspLib.VALUE_FORMSTATE_CREATE.equals(formState))
      funcName = ViewUser.FUNCTION_CREATE;
    else if (JspLib.VALUE_FORMSTATE_EDIT.equals(formState))
      funcName = ViewUser.FUNCTION_EDIT;
    else if (JspLib.VALUE_FORMSTATE_VIEW.equals(formState))
      funcName = ViewUser.FUNCTION_VIEW;
    else
      throw new Exception("未知的参数formState取值(" + formState + ")。");
    String permId = this.getPermission(funcName, org.getUuid(), isRecycle);
    if (!this.getCurrentUser().hasPerm(permId)) {
      if (directly)
        dd.addErrorText("您缺少对指定" + COrganization.CAPTION_CLASS + "进行" + funcName + "的权限。");
      if (JspLib.VALUE_FORMSTATE_EDIT.equals(formState))
        return showDetailSafely(org, JspLib.VALUE_FORMSTATE_VIEW, false);
      else
        return showListSafely(this.getFormFilter(), isRecycle);
    }
    
    return showDetail(org, formState);
  }

  private String showDetail(BOrganization org, String formState) throws Exception {
    if (org == null)
      org = getBOrgMgr().createOrganization((String) dd.getFormValue("upperOrg_uuid"));
    dd.put("org", org);

    IPermissionMgr permMgr = (IPermissionMgr) NamingMgr.getInstance().lookupMgr(
        IPermissionMgr.class);
    dd.put("am", permMgr.createFieldsAccessMode(getCurrentUser(), COrganization.class));

    Boolean isRecycle = COrganization.VALUE_STATE_DELETED.equals(org.getState()) ? Boolean.TRUE
        : Boolean.FALSE;
    if (org.getUuid() == null || "".equals(org.getUuid())) {
      int relationship = IBOrganizationMgr.VALUE_RELATIONSHIP_OTHER;
      if (getCurrentOrganization() != null) {
        BOrganization currOrg = getBOrgMgr().loadOrganization(getCurrentOrganization().getUuid());
        relationship = getBOrgMgr().getRelativePosition(currOrg, org);
      }
      this.putFormPermissions(relationship, isRecycle);
    } else
      this.putFormPermissions(org.getUuid(), isRecycle);
    dd.putFormValue(JspLib.ID_FORMSTATE, formState);
    if (JspLib.VALUE_FORMSTATE_CREATE.equals(formState))
      dd.setWorkingTemplate(TEMPLATE_CREATE);
    else if (JspLib.VALUE_FORMSTATE_EDIT.equals(formState))
      dd.setWorkingTemplate(TEMPLATE_EDIT);
    else {
      dd.setWorkingTemplate(TEMPLATE_VIEW);
      ViewEntityLogs.preloadJSP(dd, org.getUuid(), COrganization.STATEMAP);
    }
    return ActionConstants.APPLICATIONFRAME;
  }

  private void readFormOrganization(BOrganization org, String formState) throws Exception {
    String ocaStr = (String) dd.getFormValue("org_oca");
    org.setOca((new Long(ocaStr)).longValue());
    org.setCode((String) dd.validateFormValue("org_code", null, true, org.getCode(),
        COrganization.CAPTION_CODE));
    org.setName((String) dd.validateFormValue("org_name", null, true, org.getName(),
        COrganization.CAPTION_NAME));
    org.setRemark((String) dd.validateFormValue("org_remark", null, false, org.getRemark(),
        COrganization.CAPTION_REMARK));
  }

  private void putFormPermissions(String orgUuid, Boolean isRecycle) throws BusinessException,
      HibernateException {
    String permId = null;
    permId = this.getPermission(ViewOrganization.FUNCTION_CREATE, orgUuid, isRecycle);
    if (permId != null)
      dd.put("permissionCreate", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_DELETE, orgUuid, isRecycle);
    if (permId != null)
      dd.put("permissionDelete", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_UNDELETE, orgUuid, isRecycle);
    if (permId != null)
      dd.put("permissionUndelete", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_VIEW, orgUuid, isRecycle);
    if (permId != null)
      dd.put("permissionView", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_EDIT, orgUuid, isRecycle);
    if (permId != null)
      dd.put("permissionEdit", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_VIEW, orgUuid, new Boolean(!isRecycle
        .booleanValue()));
    if (permId != null)
      dd.put("permissionAnotherView", permId);
  }

  private void putFormPermissions(int relationship, Boolean isRecycle) throws BusinessException,
      HibernateException {
    String permId = null;
    permId = this.getPermission(ViewOrganization.FUNCTION_CREATE, relationship, isRecycle);
    if (permId != null)
      dd.put("permissionCreate", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_DELETE, relationship, isRecycle);
    if (permId != null)
      dd.put("permissionDelete", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_UNDELETE, relationship, isRecycle);
    if (permId != null)
      dd.put("permissionUndelete", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_VIEW, relationship, isRecycle);
    if (permId != null)
      dd.put("permissionView", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_EDIT, relationship, isRecycle);
    if (permId != null)
      dd.put("permissionEdit", permId);
    permId = this.getPermission(ViewOrganization.FUNCTION_VIEW, relationship, new Boolean(
        !isRecycle.booleanValue()));
    if (permId != null)
      dd.put("permissionAnotherView", permId);
  }

  private boolean checkToViewOrganization(BOrganization borg) throws BusinessException,
      HibernateException {
    Boolean isRecycle = new Boolean(COrganization.VALUE_STATE_DELETED.equals(borg.getState()));
    String perm = this.getPermission(ViewOrganization.FUNCTION_VIEW, borg.getUuid(), isRecycle);
    return dd.getCurrentUser().hasPermission(perm);
  }

}
